﻿using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.Domains.WarehouseCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SalesManagementCommon.API.SalesManagementCommons.SalesOrderSearch;
using SqlSugar;
using System.Reflection.Metadata;

namespace SalesManagementCommon.API.SalesManagementCommons.OutOfStock
{
    public class OutOfStockQueryHander : IRequestHandler<OutOfStockQueryCommand, Result<PageResultDto<OutOfStockaaawwwDTO>>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_db"></param>
        public OutOfStockQueryHander(ISqlSugarClient _db)
        {
            this._db = _db;
        }
        /// <summary>
        /// 处理销售订单出库命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<OutOfStockaaawwwDTO>>> Handle(OutOfStockQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<OutOfStockaaawwwDTO>>();
            var page = new PageResultDto<OutOfStockaaawwwDTO>() { TotalCount = 0, TotalPage = 0, List = new List<OutOfStockaaawwwDTO>() };
            try
            {
                RefAsync<int> totaCount = 0;
                RefAsync<int> totalPage = 0;
                var list = _db.Queryable<SaleOutOfWarehouse, SellOutMaterials>((s, sm) => s.SerialNumber == sm.SerialNumber).Select((s, sm) => new OutOfStockaaawwwDTO()
                {
                    SaleOutOfWarehouseId = s.SaleOutOfWarehouseId,
                    DocumentNumber = s.DocumentNumber,
                    DocumentDate = s.DocumentDate,
                    DocumentStatus = s.DocumentStatus,
                    DocumentType = s.DocumentType,
                    BusinessType = s.BusinessType,
                    BusinessStatus = s.BusinessStatus,
                    BusinessUnit = s.BusinessUnit,
                    BusinessPersonnel = s.BusinessPersonnel,
                    Period = s.Period,
                    WarehouseId = s.WarehouseId,
                    InvoiceNumber = s.InvoiceNumber,
                    ShippingType = s.ShippingType,
                    ModeDelivery = s.ModeDelivery,
                    CustomerProfileName = s.CustomerProfileName,
                    NoteInformation = s.NoteInformation,
                    OperatorId = s.OperatorId,
                    ItemBianma = sm.ItemBianma,
                    MaterialName = sm.MaterialName,
                    TypeSpecification = sm.TypeSpecification,
                    OutboundQuantity = sm.OutboundQuantity,
                    LotNumber = sm.LotNumber,
                    AuditDate = s.AuditDate,
                    Remark = sm.Remark, 
                }).MergeTable();
                if (!string.IsNullOrEmpty(request.DocumentNumber)) //单据编码
                {
                    list = list.Where(c => c.DocumentNumber == request.DocumentNumber);
                }
                if (!string.IsNullOrEmpty(request.ItemBianma))//物料编码
                {
                    list = list.Where(c => c.ItemBianma == request.ItemBianma);
                }
                if (!string.IsNullOrEmpty(request.MaterialName))//物料名称
                {
                    list = list.Where(c => c.MaterialName.Contains(request.MaterialName));
                }
                if (!string.IsNullOrEmpty(request.BusinessStatus))//业务状态
                {
                    list = list.Where(c => c.BusinessStatus == request.BusinessStatus);
                }
                if (!string.IsNullOrEmpty(request.DocumentStatus))//单据状态
                {
                    list = list.Where(c => c.DocumentStatus == request.DocumentStatus);
                }
                var res = await list.ToPageListAsync(request.PageIndex, request.PageSize, totaCount, totalPage);
                page.TotalCount = totaCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取当前条件下销售出库订单信息成功！";
                }
                else
                {
                    result.Data = new PageResultDto<OutOfStockaaawwwDTO>() { List = new List<OutOfStockaaawwwDTO>() };
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
